#!/bin/sh
# PCP QA Test No. 1370
# Check long-options in manual pages.
#
# Copyright (c) 2021 Red Hat.
#
# check-group-exclude: pmrep pmpost

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

which man > /dev/null 2>&1 || _notrun "No man(1) command?"
man pmrep | grep -q 'performance metrics reporter' || _notrun "No man pages installed?"

status=1        # failure is the default!
hostname=`hostname`
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

# need to check dpkg first, because if it is there, it is likely the
# one, as opposed to rpm which may be present, even on Debian machines
#
rm -f $tmp.cmds.txt
if which dpkg >/dev/null 2>&1
then
    dpkg -l 2>>$seq_full | $PCP_AWK_PROG '$1 = "ii" && $2 ~ /pcp/ { print $2 }' | xargs dpkg -L | grep bin/ | sort | uniq > $tmp.cmds.txt
elif which rpm >/dev/null 2>&1
then
    rpm -qa 2>>$seq_full | grep pcp | xargs rpm -ql | grep bin/ | grep -v 'debug$' | sort > $tmp.cmds.txt
fi
if [ ! -s $tmp.cmds.txt ]
then
    _notrun "neither rpm nor dpkg seem to be your packaging command"
fi

# real QA test starts here
echo "collecting commands"
export LC_COLLATE=POSIX

# don't bother for some commands, either they don't support --help
# (e.g. no options to describe) and/or are not intended for general
# use by a human end user and/or are aliases for other commands
#
for cmd in pcp-htop pcp-python pcp2csv pcp2xlsx pmafm pmgenmap \
             pmie_dump_stats pmiestatus pmlock pmpause pmpython \
             pmsleep pmwtf telnet-probe pmpost pcp-kube-pods \
	     pmie_farm pmlogger_farm pmlogger_janitor \
	     pcp-geolocate pcp-reboot-init pmcheck pmlogbasename \
	     pmdbg
do
  sed -i "/$cmd/d" $tmp.cmds.txt
done

ncmd=`wc -l <$tmp.cmds.txt | sed -e 's/  *//'`
if [ "$ncmd" -lt 100 ]
then
    echo "Warning: only found $ncmd candidate installed commands, expecting at least 100"
fi

# borrowed from _check_display()
# if we can't talk to an X server, there is no point running
# --help for the GUI apps
#
X11=true
if [ -z "$PCPQA_CLOSE_X_SERVER" ]
then
    X11=false
fi
export DISPLAY=$PCPQA_CLOSE_X_SERVER
if which xdpyinfo >/dev/null 2>&1
then
    xdpyinfo >/dev/null 2>&1 || \
	X11=false
fi

export PCP_STDERR=$tmp.popup

echo "run checks"
for cmd in $(cat $tmp.cmds.txt); do
  rm -f $tmp.test
  bn=$(basename $cmd)
  rm -f $tmp.popup
  if ! $X11
  then
    case $cmd
    in
	*/pmchart|*/pmtime|*/pmconfirm|*/pmquery|*/pmclient_fg|*/pmmessage)
	    # for these ones --help will try to open a GUI popup or
	    # at least start an X11 client setup and without an X11
	    # server that ain't going to work
	    #
	    continue
	    ;;
    esac
  fi
  $cmd --help >$tmp.out 2>$tmp.err
  [ -f $tmp.popup ] && cat $tmp.popup >>$tmp.err
  # if --help is not understood, usually get the "unrecognized
  # option" message, followed by the usage message
  #
  cat $tmp.out $tmp.err | grep 'unrecognized option'
  cat $tmp.out $tmp.err \
  | grep -Eo -- '--.*( |	|$)' \
  | sed \
      -e '/unrecognized option/d' \
      -e 's/=.*//g' \
      -e 's/[ 	].*//g' \
      -e 's/]//g' \
      -e 's/)//g' \
  | LC_COLLATE=POSIX sort \
  | uniq \
  | grep -e '-[A-Za-z]' > $tmp.test
  if ! grep -q -- -- $tmp.test; then
    continue
  fi
  echo "=== $cmd ===" >>$seq_full
  cat $tmp.test >>$seq_full
  # run man(1) once, then grep away
  man $bn >$tmp.man 2>$tmp.err
  if [ -s $tmp.err ]
  then
    echo " == $cmd ... error output from man(1) ==="
    cat $tmp.err
  fi
  for opt in $(cat $tmp.test); do
    # some --help text is misleading, not really options following
    # -- ... skip 'em
    case "$bn"
    in
	pcp-atop)
	    [ X"$opt" = X--pcp-flags ] && continue
	    ;;
	pcp-xsos)
	    [ X"$opt" = X--start ] && continue
	    ;;
	pcp)
	    # all these long options are documented in PCPIntro(1)
	    # and pcp(1) points there, rather than repeating them
	    #
	    [ X"$opt" = X--align ] && continue
	    [ X"$opt" = X--finish ] && continue
	    [ X"$opt" = X--guimode ] && continue
	    [ X"$opt" = X--guiport ] && continue
	    [ X"$opt" = X--hostzone ] && continue
	    [ X"$opt" = X--interval ] && continue
	    [ X"$opt" = X--samples ] && continue
	    [ X"$opt" = X--start ] && continue
	    [ X"$opt" = X--timezone ] && continue
	    ;;
	pmproxy)
	    [ X"$opt" = X--redishost ] && continue
	    [ X"$opt" = X--redisport ] && continue
	    ;;
    esac
    grep -q -- "$opt" <$tmp.man || echo "$bn.1 missing $opt"
  done
  rm -f $tmp.test
done
echo "checks done"

# success, all done
status=0
exit
